/*
 * Copyright [2013-2021], Alibaba Group Holding Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.alibaba.polardbx.common.utils.time.parser;

import com.alibaba.polardbx.common.utils.time.MySQLTimeTypeUtil;

import java.sql.Types;


public class TimeParseStatus {
    public enum StatusType {

        NONE,

        ERROR,

        DATE,
        DATETIME,
        TIME;

        public static StatusType of(int sqlType) {
            switch (sqlType) {
            case Types.TIMESTAMP:
            case MySQLTimeTypeUtil.DATETIME_SQL_TYPE:
                return DATETIME;
            case Types.DATE:
                return DATE;
            case Types.TIME:
                return TIME;
            default:
                return NONE;
            }
        }
    }

    private int warnings;
    private StatusType status;

    private int nano;

    private int digitsOfFractional;

    public TimeParseStatus() {
        warnings = 0;
        status = StatusType.NONE;
        nano = 0;
        digitsOfFractional = 0;
    }

    public void clear() {
        warnings = 0;
        status = StatusType.NONE;
        nano = 0;
        digitsOfFractional = 0;
    }

    public void addWarning(int warning) {
        warnings |= warning;
    }

    public int getWarnings() {
        return warnings;
    }

    public StatusType getStatus() {
        return status;
    }

    public void setStatus(StatusType status) {
        this.status = status;
    }

    public int getNano() {
        return nano;
    }

    public void setNano(int nano) {
        this.nano = nano;
    }

    public int getDigitsOfFractional() {
        return digitsOfFractional;
    }

    public void setDigitsOfFractional(int digitsOfFractional) {
        this.digitsOfFractional = digitsOfFractional;
    }

    public boolean checkWarnings(int flag) {
        return TimeParserFlags.check(flag, warnings);
    }
}
